package it.unibo.mumu.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import it.unibo.mumu.entity.Universe;

public class ParseXmlUniverse {
    List<Universe> universes;
    private Universe universe;
    private String text;
 
    public ParseXmlUniverse() {
        universes = new ArrayList<Universe>();
    }
 
    public List<Universe> getUniverses() {
        return universes;
    }
 
    public List<Universe> parse(InputStream is) {
        XmlPullParserFactory factory = null;
        XmlPullParser parser = null;
        try {
            factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            parser = factory.newPullParser();
 
            parser.setInput(is, null);
 
            int eventType = parser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                String tagname = parser.getName();
                switch (eventType) {
                case XmlPullParser.START_TAG:
                    if (tagname.equalsIgnoreCase("universe")) {
                        // create a new instance of employee
                        universe = new Universe();
                        
                        // set attributes
                        universe.setId(Integer.parseInt(parser.getAttributeValue(null, "id")));
                        universe.setName(parser.getAttributeValue(null, "name"));
                    }
                    break;
 
                case XmlPullParser.TEXT:
                    text = parser.getText();
                    break;
 
                case XmlPullParser.END_TAG:
                    if (tagname.equalsIgnoreCase("universe")) {
                        // add employee object to list
                    	
//                    	Log.i("VED_id", parser.getAttributeValue(null, "id"));
//                    	Log.i("VED_name", parser.getAttributeValue(null, "name"));
                        universes.add(universe);
                        SingletonSupport support = SingletonSupport.getInstance();
                        support.addSimpleUniverse(universe);
                    } else if (tagname.equalsIgnoreCase("description")) {
                    	//Log.i("P1", text);
                        universe.setDescription(text);
                    } else if (tagname.equalsIgnoreCase("UUID")) {
                        universe.setUUID(text);//.setId(Integer.parseInt(text));
                    } else if (tagname.equalsIgnoreCase("numplanets")) {
                        universe.setNumplanets(Integer.parseInt(text));//.setDepartment(text);
                    } else if (tagname.equalsIgnoreCase("size")) {
                        universe.setSize(text);
                    } else if (tagname.equalsIgnoreCase("avatarpath")) {
                        universe.setAvatarpath(text);
                    }
                    break;
 
                default:
                    break;
                }
                eventType = parser.next();
            }
 
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
        return universes;
    }
}

